package org.grow.yc.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.grow.yc.filter.AuthInterceptor;
import org.grow.yc.filter.ExampleFilter;
import org.grow.yc.filter.SampleFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.io.File;

/**
 * @Author: xwg
 * @CreateDate: 2024/9/27
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${my-upload}")
    private String uploadDir;

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .order(1)
                .excludePathPatterns("/swagger*/**", "/v3/**", "/staticWeb/**","/pwd/login")
                .excludePathPatterns("/employee*/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        String separator = File.separator;
        if (!uploadDir.startsWith(separator)) {
            uploadDir = separator + uploadDir;
        }
        if (!uploadDir.endsWith(separator)) {
            uploadDir = uploadDir + separator;
        }
        System.out.println("修缮后的" + uploadDir);
        registry.addResourceHandler("/staticWeb/**")
//              windows  "file://"  + "/D:/xxx/"
                .addResourceLocations("file://" + uploadDir);
    }

    //    @Bean
//    bean注解作用也是将方法返回值对象 放入spring容器 与component service 类似
    public FilterRegistrationBean<Filter> exampleFilterRegistration(ExampleFilter exampleFilter) {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(exampleFilter);
        bean.setOrder(100);
        return bean;
    }

    //    @Bean
//    bean注解作用也是将方法返回值对象 放入spring容器 与component service 类似
    public FilterRegistrationBean<Filter> sampleFilterRegistration(SampleFilter sampleFilter) {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(sampleFilter);
        bean.setOrder(200);
        return bean;
    }

    //    AOP编程的思维 Filter是一种体现
    @Bean
    public OpenAPI api() {
        return new OpenAPI()
                .addSecurityItem(new SecurityRequirement().addList("Bearer Authorization"))
                .components(components());
    }

    private Components components() {
        return new Components()
                .addSecuritySchemes("Bearer Authorization",
                        new SecurityScheme()
                                .name("Bearer token auth")
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")
                                .in(SecurityScheme.In.HEADER));
    }
}
